package
{
	import flash.display.*;
	import flash.utils.ByteArray;
	import flash.utils.getDefinitionByName;
	import cmodule.jpeg.CLibInit;
	public class Jpeg
	{
		private static var lib:Object = null;
		public static function encode(bmpd:BitmapData,_quality:int=80,_bytes:ByteArray=null):ByteArray
		{
			var bytes:ByteArray = null;
			if(lib==null)
			{
				try{
					//	compressor:Object — 要使用的压缩程序类型。有效值为：flash.display.PNGEncoderOptions、flash.display.JPEGEncoderOptions 和 flash.display.JPEGXREncoderOptions。
					var ClassReference:Class = getDefinitionByName("flash.display.JPEGEncoderOptions") as Class;
					var compressor:Object = new ClassReference(_quality);
					bytes = bmpd["encode"](bmpd.rect, compressor, _bytes); 
					return bytes;
				}catch(e:Error){
					logs.adds(e);
				}
			}
			try{
				if(lib==null)
					lib = new cmodule.jpeg.CLibInit().init();
				var bmpdBytes:ByteArray = bmpd.getPixels(bmpd.rect);
				bytes = lib.encode(bmpd.width,bmpd.height,bmpdBytes,_quality);
			}catch(e2:Error){
				Toast.show(e2.message);
			}
			return bytes;
		}
	}
}

